package com.example.android.ui.home;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.util.Pair;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.android.R;
import com.example.android.adapter.CarReleaseAdapter;

import com.example.android.adapter.ImageAdapter;
import com.example.android.adapter.ImageTitleNumAdapter;
import com.example.android.bean.CarOrder;
import com.example.android.bean.CarRelease;
import com.example.android.bean.NewsBean;

import com.example.android.databinding.FragmentHomeBinding;
import com.example.android.utils.BaseModel;
import com.example.android.utils.ImageUrlUtil;
import com.example.android.utils.NetUtil;
import com.example.android.utils.StringUtil;
import com.example.android.utils.api.CarOrderApi;
import com.example.android.utils.viewutils.ReservePopup;
import com.google.android.material.datepicker.CalendarConstraints;
import com.google.android.material.datepicker.MaterialDatePicker;
import com.google.android.material.datepicker.MaterialPickerOnPositiveButtonClickListener;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;
    private CarReleaseAdapter carReleaseAdapter;

    private Banner<?, BannerAdapter<?, ?>> banner;


    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        HomeViewModel homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        //设置首页刷新
        initRefreshLayout(homeViewModel, root);
        //设置view
        initRecyclerView(root);
        initBanner(inflater, container);

        getNewsList(homeViewModel);
        getAdList(homeViewModel);


        return root;
    }

    private void initBanner(@NonNull LayoutInflater inflater, ViewGroup container) {
        View headerView = inflater.inflate(R.layout.header_home, container, false);

        carReleaseAdapter.setHeaderView(headerView);
        carReleaseAdapter.setHeaderWithEmptyEnable(true);
        banner = headerView.findViewById(R.id.banner);
        banner.addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(getContext())).start();
        //设置默认的图片
        List<Integer> list = new ArrayList<>();
        list.add(R.drawable.pic_item_list_default);
        list.add(R.drawable.pic_item_list_default);
        list.add(R.drawable.pic_item_list_default);
        banner.setAdapter(new ImageAdapter(list));
    }

    private void initRecyclerView(View root) {
        RecyclerView recyclerView = root.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        carReleaseAdapter = new CarReleaseAdapter(null);
        recyclerView.setAdapter(carReleaseAdapter);
        carReleaseAdapter.setEmptyView(R.layout.empty_home);
        //点击预约事件
        carReleaseAdapter.addChildClickViewIds(R.id.yuyue, R.id.profile_image);
        carReleaseAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            CarRelease item = (CarRelease) adapter.getItem(position);
           //点击按钮操作
            switch (view.getId()) {
                case R.id.yuyue:

                    //弹窗
                    ReservePopup reservePopup = new ReservePopup(getContext(),item);
                    reservePopup.setPopupGravity(Gravity.CENTER);
                    reservePopup.showPopupWindow();
                    //String转Long


                    break;
                case R.id.profile_image:
                    String imagesUrl = ImageUrlUtil.getImagesUrl(item.getCarInfo());
                    if (imagesUrl != null) {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", imagesUrl);
                        Navigation.findNavController(view).navigate(R.id.action_navigation_home_to_webFragment, bundle);
                    }
                    break;
                default:
            }

        });

    }

    private void initRefreshLayout(HomeViewModel homeViewModel, View root) {
        RefreshLayout refreshLayout = (RefreshLayout) root.findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            getNewsList(homeViewModel);
            getAdList(homeViewModel);
            refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
        });

        refreshLayout.setOnLoadMoreListener(refreshlayout -> {
            refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            Toast.makeText(getActivity(), "没有更多数据了", Toast.LENGTH_SHORT).show();
        });
    }

    private void getNewsList(HomeViewModel homeViewModel) {
        homeViewModel.getCarRelease().observe(getViewLifecycleOwner(), (carReleases -> {
            if (carReleases != null && carReleases.getRows() != null) {
                carReleaseAdapter.setList(carReleases.getRows());
            }
        }));

    }

    private void getAdList(HomeViewModel homeViewModel) {
        homeViewModel.getAdList().observe(getViewLifecycleOwner(), (adBeanList) -> {
            banner.setAdapter(new ImageTitleNumAdapter(adBeanList));
            banner.setOnBannerListener((data, position) -> {
                Bundle bundle = new Bundle();
                bundle.putString("url", ((NewsBean) data).getNewsUrl());
                Navigation.findNavController(banner).navigate(R.id.action_navigation_home_to_webFragment, bundle);
            });
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}